/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ds_semetralka;

import entities.Faculty;
import entities.Person;
import entities.Program;
import entities.Student;
import entities.Subject;
import entities.University;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Lingi
 */
public class Generate {

    EntityManager em;
    EntityTransaction tx;

    public Generate(String persistenceUnit) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
        this.em = emf.createEntityManager();
        this.tx = em.getTransaction();

        generateUniversities();
    }

    public void generateUniversities() {
        University cvut = new University("CVUT", "Praha");

        generateFaculties(cvut);


        tx.begin();
        em.persist(cvut);
        tx.commit();
    }

    public void generateFaculties(University uni) {
        Faculty fel = new Faculty("Fakulta elektrotechnicka", "Technicka 2");
        Faculty fit = new Faculty("Fakulta informacnich technologii", "Kolejni 550/2");
        Faculty fs = new Faculty("Fakulta strojni", "Technicka 4");

        List<Faculty> faculties = new ArrayList();
        faculties.add(fel);
        faculties.add(fit);
        faculties.add(fs);

        fel.setUniversity(uni);
        fit.setUniversity(uni);
        fs.setUniversity(uni);

        uni.setFaculties(faculties);


        generatePrograms(fel);
        generatePrograms(fit);
        generatePrograms(fs);


        tx.begin();
        em.persist(fel);
        em.persist(fit);
        em.persist(fs);
   //     em.refresh(uni);
        tx.commit();
    }

    public void generatePrograms(Faculty fa) {
        List<Program> programs = new ArrayList();

        if (fa.getName().equals("Fakulta elektrotechnicka")) {
            Program oi = new Program("Otevrena Informatika");
            Program kyr = new Program("Kybernetika a Robotika");

            programs.add(oi);
            programs.add(kyr);

            oi.setFaculty(fa);
            kyr.setFaculty(fa);
            fa.setPrograms(programs);


            generateStudents(kyr);
            generateStudents(oi);
            

            tx.begin();
            em.persist(oi);
            em.persist(kyr);
       //     em.refresh(fa);
            tx.commit();

        }

        if (fa.getName().equals("Fakulta informacnich technologii")) {
            Program si = new Program("Softwarove inzenyrstvi");
            Program ti = new Program("Teoreticka informatika");

            programs.add(si);
            programs.add(ti);

            si.setFaculty(fa);
            ti.setFaculty(fa);
            fa.setPrograms(programs);


            generateStudents(si);
            generateStudents(ti);

            tx.begin();
            em.persist(si);
            em.persist(ti);
        //    em.refresh(fa);
            tx.commit();
        }

        if (fa.getName().equals("Fakulta strojni")) {
            Program str = new Program("Strojirenstvi");

            programs.add(str);

            str.setFaculty(fa);
            fa.setPrograms(programs);
            
            generateStudents(str);

            generateStudents(str);

            tx.begin();
            em.persist(str);
        //    em.refresh(fa);
            tx.commit();
        }
    }

    public void generateStudents(Program prg) {
        List<Student> students = new ArrayList<Student>();
        Random rand = new Random();

        ArrayList<String> firstNames = new ArrayList<String>();
        firstNames.add("Josef");
        firstNames.add("Karel");
        firstNames.add("Jiri");
        firstNames.add("Martin");
        firstNames.add("Robert");
        ArrayList<String> lastNames = new ArrayList<String>();
        lastNames.add("Novak");
        lastNames.add("Harant");
        lastNames.add("Stejskal");
        lastNames.add("Ruzicka");
        lastNames.add("Burianek");

        Person[] persons = new Person[20];

        for (int i = 0; i < persons.length; i++) {
            persons[i] = new Person("1234", firstNames.get(rand.nextInt(firstNames.size())), lastNames.get(rand.nextInt(lastNames.size())));
            Student st = new Student();
            st.setProgram(prg);
            st.setPerson(persons[i]);
            students.add(st);
            persons[i].setStudent(st);
        }
        prg.setStudents(students);


        generateSubjects(students);

        tx.begin();
        for (int i = 0; i < persons.length; i++) {
            em.persist(persons[i]);
        }
        for (Student stud : students) {
            em.persist(stud);
        }
     //   em.refresh(prg);
        tx.commit();
    }

    public void generateSubjects(List<Student> students) {
        List<Subject> subjects = new ArrayList<Subject>();

        Subject sub1;
        Subject sub2;
        Subject sub3;
        Subject sub4;
        Subject sub5;

        Student student = students.get(0);

//        tx.begin();
//
//        for (Student stud : students) {
//            em.refresh(stud);
//        }
//
//        tx.commit();

        if (student.getProgram().getName().equals("Otevrena Informatika")) {
            sub1 = new Subject("Linearni Algebra", 300, (short) 7);
            sub2 = new Subject("Diskretni Matematika", 250, (short) 7);
            sub3 = new Subject("Programovani 1", 250, (short) 6);
            sub4 = new Subject("Reseni Problemu a Hry", 250, (short) 6);
            sub5 = new Subject("Logika a Grafy", 250, (short) 5);
        } else if (student.getProgram().getName().equals("Kybernetika a Robotika")) {
            sub1 = new Subject("Kybernetika", 200, (short) 6);
            sub2 = new Subject("Robotika", 200, (short) 7);
            sub3 = new Subject("Matematika 1", 300, (short) 8);
            sub4 = new Subject("Elektricke Obvody", 250, (short) 6);
            sub5 = new Subject("Umela Inteligence", 150, (short) 5);
        } else if (student.getProgram().getName().equals("Softwarove inzenyrstvi")) {
            sub1 = new Subject("Matematicka Analyza", 300, (short) 8);
            sub2 = new Subject("Softwarove Inzenyrstvi", 250, (short) 7);
            sub3 = new Subject("Architektura Pocitacu", 250, (short) 8);
            sub4 = new Subject("Databazove Systemy", 250, (short) 6);
            sub5 = new Subject("Ekonomika", 250, (short) 5);
        } else if (student.getProgram().getName().equals("Teoreticka informatika")) {
            sub1 = new Subject("Teorie Informace", 200, (short) 8);
            sub2 = new Subject("Automaty a Gramatiky", 250, (short) 7);
            sub3 = new Subject("Struktura Pocitacovych Systemu", 250, (short) 8);
            sub4 = new Subject("Architektura Softwarovych Systemu", 250, (short) 6);
            sub5 = new Subject("Anglicky Jazyk", 250, (short) 5);
        } else {
            sub1 = new Subject("Technicke Kresleni", 200, (short) 6);
            sub2 = new Subject("Fyzika 1", 250, (short) 7);
            sub3 = new Subject("Mechanika", 250, (short) 8);
            sub4 = new Subject("Dynamicke Systemy", 250, (short) 6);
            sub5 = new Subject("Historie Techniky", 250, (short) 5);
        }

        subjects.add(sub1);
        subjects.add(sub2);
        subjects.add(sub3);
        subjects.add(sub4);
        subjects.add(sub5);

        System.out.println(students);

        for (Student stud : students) {
            System.out.println(stud);
            stud.setSubjects(subjects);
            sub1.addStudent(stud);
            sub2.addStudent(stud);
            sub3.addStudent(stud);
            sub4.addStudent(stud);
            sub5.addStudent(stud);
        }

        Faculty faculty = student.getProgram().getFaculty();
        faculty.addSubject(sub1);
        faculty.addSubject(sub2);
        faculty.addSubject(sub3);
        faculty.addSubject(sub4);
        faculty.addSubject(sub5);

        sub1.setFaculty(faculty);
        sub2.setFaculty(faculty);
        sub3.setFaculty(faculty);
        sub4.setFaculty(faculty);
        sub5.setFaculty(faculty);

        tx.begin();
        em.persist(sub1);
        em.persist(sub2);
        em.persist(sub3);
        em.persist(sub4);
        em.persist(sub5);
        for (Student stud : students) {
            em.refresh(stud);
        }
//        em.refresh(student.getProgram().getFaculty());
        tx.commit();
    }
}
